export const Modal = {
	Alert: (content, title = "提示") => {
		return new Promise((resolve, reject) => {
			wx.showModal({
				title,
				content,
				showCancel: false,
				confirmColor: "#2d8cf0",
				success: res => {
					resolve(res.confirm)
				},
				false: res => {reject(res)}
			})
		})
	},
	Confirm: (content, title = "提示") => {
		return new Promise((resolve, reject) => {
			wx.showModal({
				title,
				content,
				confirmColor: "#2d8cf0",
				success: res => {
					resolve(res.confirm)
				},
				false: res => {reject(res)}
			})
		})
	},
	showLoading: (title = "载入中") => {
		wx.showLoading({
			title,
			mask: true
		})
	},
	hideLoading: () => {
		wx.hideLoading()
	}
}

export const Nav = {
	setTitle: title => {
		wx.setNavigationBarTitle({title})
	}
}

export default function deepCopy(obj, cache = []) {

  function find(list, f) {
    return list.filter(f)[0]
  }

  // just return if obj is immutable value
  if (obj === null || typeof obj !== 'object') {
    return obj
  }

  // if obj is hit, it is in circular structure
  const hit = find(cache, c => c.original === obj)
  if (hit) {
    return hit.copy
  }

  const copy = Array.isArray(obj) ? [] : {}
  // put the copy into cache at first
  // because we want to refer it in recursive deepCopy
  cache.push({
    original: obj,
    copy
  })

  Object.keys(obj).forEach(key => {
    copy[key] = deepCopy(obj[key], cache)
  })

  return copy
}
